$(function () {
    $("#jqGrid").jqGrid({
        url: '../customerManager/list',
        datatype: "json",
        colModel: [
			{label: 'customerId', name: 'customerId', index: 'customer_id', key: true, hidden: true},
			{label: '客户编码', name: 'customerCode', index: 'customer_code', width: 80},
			{label: '客户中文名称', name: 'customerName', index: 'customer_name', width: 80},
			{label: '客户英文名称', name: 'customerEnglishName', index: 'customer_english_name', width: 80},
			{label: '客户简称', name: 'customerAbbreviation', index: 'customer_abbreviation', width: 80},
			{label: '客户类型', name: 'customerType', index: 'customer_type', width: 80},
			{label: '公司联络电话', name: 'customerTel', index: 'customer_tel', width: 80},
			{label: '所属业务员', name: 'customerSalesman', index: 'customer_salesman', width: 80},
			{label: '客户性质', name: 'customerNature', index: 'customer_nature', width: 80},
			{label: '是否有效', name: 'isValid', index: 'is_valid', width: 80},
			{label: '主管机构', name: 'competentAuthority', index: 'competent_authority', width: 80},
			{label: '电子邮件', name: 'email', index: 'email', width: 80},
			{label: '网址', name: 'url', index: 'url', width: 80},
			{label: '纳税识别号', name: 'taxNumber', index: 'tax_number', width: 80},
			{label: '经营执照号码', name: 'businessNumber', index: 'business_number', width: 80},
			{label: '固定电话', name: 'telephone', index: 'telephone', width: 80},
			{label: '移动电话', name: 'phone', index: 'phone', width: 80},
			{label: '传真', name: 'fax', index: 'fax', width: 80},
			{label: '邮政编码', name: 'postalCode', index: 'postal_code', width: 80},
			{label: '联系地址', name: 'address', index: 'address', width: 80},
			{label: '会员类别', name: 'memberType', index: 'member_type', width: 80},
			{label: '会员卡号', name: 'memberCard', index: 'member_card', width: 80},
			{label: '会员级别', name: 'memberLevel', index: 'member_level', width: 80},
			{label: '会籍开始时间', name: 'memberAddtime', index: 'member_addtime', width: 80},
			{label: '会籍结束时间', name: 'memberEndtime', index: 'member_endtime', width: 80},
			{label: '一级业务', name: 'firstLevelBusiness', index: 'first_level_business', width: 80},
			{label: '二级业务', name: 'twLevelBusiness', index: 'tw_level_business', width: 80},
			{label: '主营业务', name: 'mainBusiness', index: 'main_business', width: 80},
			{label: '主营业务描述', name: 'description', index: 'description', width: 80},
			{label: '添加时间', name: 'addtime', index: 'addtime', width: 80},
			{label: '修改时间', name: 'mofidytime', index: 'mofidytime', width: 80}],
		viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
	el: '#rrapp',
	data: {
        showList: true,
        title: null,
		tCustomerManager: {},
		ruleValidate: {
			name: [
				{required: true, message: '名称不能为空', trigger: 'blur'}
			]
		},
		q: {
		    name: ''
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function () {
			vm.showList = false;
			vm.title = "新增";
			vm.tCustomerManager = {};
		},
		update: function (event) {
            let customerId = getSelectedRow();
			if (customerId == null) {
				return;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(customerId)
		},
		saveOrUpdate: function (event) {
            let url = vm.tCustomerManager.customerId == null ? "../customerManager/save" : "../tcustomerManager/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.tCustomerManager),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
			});
		},
		del: function (event) {
            let customerIds = getSelectedRows();
			if (customerIds == null){
				return;
			}

			confirm('确定要删除选中的记录？', function () {
				$.ajax({
					type: "POST",
				    url: "../customerManager/delete",
				    contentType: "application/json",
				    data: JSON.stringify(customerIds),
				    success: function (r) {
						if (r.code == 0) {
							alert('操作成功', function (index) {
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(customerId){
			$.get("../customerManager/info/"+customerId, function (r) {
                vm.tCustomerManager = r.tCustomerManager;
            });
		},
		reload: function (event) {
			vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
			$("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
		},
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
	}
});